package cn.source.common.utils;


import cn.source.common.config.RuoYiConfig;
import org.bytedeco.javacv.*;
import org.bytedeco.javacv.Frame;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class VideoUtils {
    public static void main(String[] args) {
        voidSplit("","","","","");
    }
    /**
     * 视频截取
     * @param inputFilePath 输入地址
     * @param outputFilePath 输出地址
     * @param ffPath ffmpeg地址
     * @param startTime 开始时间
     * @param duration  截取时常
     */
    public static void voidSplit(String inputFilePath,String outputFilePath,String ffPath,String startTime,String duration){
//        inputFilePath = "http://image.yianjiaoyu168.com/d77db1de-dd63-4c48-9e2f-a1cbbdbc7d08.mp4";   // 输入视频文件路径
//        outputFilePath = "D:/bendi/sp/upload/2023/08/06/11.mp4"; // 输出截取后的视频文件路径
//
////        getFrameFile(inputFilePath,"D:/bendi/sp/upload/2023/08/06/aa.jpg");
//        startTime = "00:00:02";        // 截取开始时间
//        duration = "00:00:05";         // 截取时长
////
//        ffPath = "D:\\java_project\\ffmpeg\\ffmpeg-git-full_v5.1.2\\ffmpeg-2023-02-09-git-159b028df5-full_build\\bin\\ffmpeg.exe";
        // 构建FFmpeg命令
        String[] command = {ffPath, "-i", inputFilePath, "-ss", startTime, "-t", duration, "-c", "copy", outputFilePath};
        try {
            // 执行FFmpeg命令
            ProcessBuilder builder = new ProcessBuilder(command);
            Process process = builder.start();
            // 读取命令输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            // 等待命令执行完成
            int exitCode = process.waitFor();
            if (exitCode == 0) {
                System.out.println("视频截取完成！");
            } else {
                System.out.println("视频截取失败！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
    /**
     * @param videofile 视频路径，精确到文件
     * @param framefile 保存的目标路径，精确到文件
     * @return
     * @throws Exception
     */
    public static boolean getFrameFile(String videofile, String framefile) {
        try {
            File targetFile = new File(framefile);
            FFmpegFrameGrabber ff = new FFmpegFrameGrabber(videofile);
            try {
                ff.start();
                int lenght = ff.getLengthInFrames();
                int i = 0;
                Frame f = null;
                while (i < lenght) {
                    // 过滤前5帧
                    f = ff.grabFrame();
                    if ((i > 48) && (f.image != null)) {
                        break;
                    }
                    i++;
                }
                int owidth = f.imageWidth;
                int oheight = f.imageHeight;
                // 对截取的帧进行等比例缩放
                int width = 300;
                int height = (int) (((double) width / owidth) * oheight);
                Java2DFrameConverter converter = new Java2DFrameConverter();
                BufferedImage fecthedImage = converter.getBufferedImage(f);
                BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
                bi.getGraphics().drawImage(fecthedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH),
                        0, 0, null);
                ImageIO.write(bi, "jpg", targetFile);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("ImgBase64Util fetchFrame() error.");
            } finally {
                ff.stop();
                ff.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
